#include "keymanager.h"
#include <game/keyproxy.h>

KeyManager::KeyManager(QObject *parent) :
    QObject(parent)
{
}

void KeyManager::keyPressed(Qt::Key key) {
    if (!myObjects.contains(key)) {
        return;
    }
    QVector<KeyProxy *> * proxies = myObjects.value(key);
    foreach (KeyProxy *proxy, *proxies) {
        proxy->emitPressed();
    }
}

void KeyManager::keyReleased(Qt::Key key) {
    if (!myObjects.contains(key)) {
        return;
    }
    QVector<KeyProxy *> * proxies = myObjects.value(key);
    foreach (KeyProxy *proxy, *proxies) {
        proxy->emitReleased();
    }
}

void KeyManager::addEvent(Qt::Key key, QObject *obj, const char *slotPressed, const char *slotReleased) {
    KeyProxy *proxy = new KeyProxy(this);
    if (!myObjects.contains(key)) {
        QVector<KeyProxy *> * vect = new QVector<KeyProxy *>();
        vect->append(proxy);
        myObjects.insert(key, vect);
    } else {
        QVector<KeyProxy *> * proxies = myObjects.value(key);
        proxies->append(proxy);
    }

    connect(proxy, SIGNAL(eventPressed()), obj, slotPressed);
    connect(proxy, SIGNAL(eventReleased()), obj, slotReleased);
}

void KeyManager::addReleaseEvent(Qt::Key key, QObject *obj, const char *slot) {
    KeyProxy *proxy = new KeyProxy(this);
    if (!myObjects.contains(key)) {
        QVector<KeyProxy *> * vect = new QVector<KeyProxy *>();
        vect->append(proxy);
        myObjects.insert(key, vect);
    } else {
        QVector<KeyProxy *> * proxies = myObjects.value(key);
        proxies->append(proxy);
    }

    connect(proxy, SIGNAL(eventReleased()), obj, slot);
}

void KeyManager::addPressEvent(Qt::Key key, QObject *obj, const char *slot) {
    KeyProxy *proxy = new KeyProxy(this);
    if (!myObjects.contains(key)) {
        QVector<KeyProxy *> * vect = new QVector<KeyProxy *>();
        vect->append(proxy);
        myObjects.insert(key, vect);
    } else {
        QVector<KeyProxy *> * proxies = myObjects.value(key);
        proxies->append(proxy);
    }

    connect(proxy, SIGNAL(eventPressed()), obj, slot);
}
